package com.daguchuangyi.erp_android.production_detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.product.Storage;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class StockInfoFragment extends Fragment {
    private static final String TAG_STOCK_ARRAY = "TAG_STOCK_ARRAY";

    private RecyclerView mStockRv;
    private StockAdapter mStockAdapter;

    public StockInfoFragment() {
        // Required empty public constructor
    }

    public static StockInfoFragment getInstance(ArrayList<Storage> storages) {
        StockInfoFragment stockInfoFragment = new StockInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(TAG_STOCK_ARRAY, storages);
        stockInfoFragment.setArguments(bundle);
        return stockInfoFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_stock_info, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mStockRv = (RecyclerView) view.findViewById(R.id.stock_rv);
        mStockRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mStockAdapter = new StockAdapter(R.layout.item_product_detail_stock_layout, null);
        mStockRv.setAdapter(mStockAdapter);
//        mStockRv.setNestedScrollingEnabled(true);

        loadData();
    }

    private void loadData() {
        if (getArguments() == null) {
            return;
        }
        List<Storage> storages = getArguments().getParcelableArrayList(TAG_STOCK_ARRAY);
        mStockAdapter.setNewData(storages);

        View headerView = LayoutInflater.from(getContext()).inflate(R.layout.header_stock_produt_layout,
                (ViewGroup) mStockRv.getParent(), false);
        mStockAdapter.addHeaderView(headerView);
    }

    private class StockAdapter extends BaseQuickAdapter<Storage, BaseViewHolder> {

        public StockAdapter(int layoutResId, @Nullable List<Storage> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Storage item) {
            helper.setText(R.id.position_info, item.getWarehousesString() + "/" +  item.getShelvesString())
                    .setText(R.id.stock_remark_tv, item.getRemark())
                    .setText(R.id.color_info, item.getColor() + item.getColorNum())
                    .setText(R.id.amount_info, item.getStorageQuantity() + item.getUnit());
        }
    }

}
